﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
using System.Drawing;

namespace MusaDor.Publisher
{
    public partial class AddTopic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public int returnValue { get; set; }

        protected void Send_Click(object sender, EventArgs e)
        {
            Boolean Approved = false;
            String AddedBy = User.Identity.Name;
            String connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            String PreviewPath = Server.MapPath("~/Images/Sections/PreviewImages/");
            String FullPath = Server.MapPath("~/Images/Sections/FullImages/");
            Boolean PreviewFileOK = false;
            Boolean FullFileOK = false;
            Boolean PreviewImageOK = false;
            Boolean FullImageOk = false;
            Boolean PreviewImageResolution = false;
            Boolean FullImageResolution = false;

            if (IsPostBack)
            {
                if (PreviewImageUpload.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(PreviewImageUpload.FileName).ToLower();
                    String[] allowedExtensions = { ".png" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        try
                        {
                            Bitmap previewimage = new Bitmap(PreviewImageUpload.FileContent);

                            if ((previewimage.Size.Height == 200) && (previewimage.Size.Width == 200))
                            {
                                if (fileExtension == allowedExtensions[i])
                                {
                                    PreviewFileOK = true;
                                }
                            }
                            else
                            {
                                PreviewImageResolution = true;
                            }
                        }
                        catch
                        {
                            PreviewImageUploadStatus.Text = "Error";
                        }

                    }
                }

                if (PreviewFileOK)
                {
                    try
                    {
                        PreviewImageOK = true;
                    }
                    catch (Exception exp)
                    {
                        PreviewImageUploadStatus.Text = "Error";
                    }
                }
                else
                {
                    if (PreviewImageResolution)
                    {
                        PreviewImageUploadStatus.Text = "Size of miniature must be 200х200 px.";
                    }
                    else
                    {
                        PreviewImageUploadStatus.Text = "Error";
                    }
                }
            }

            if (IsPostBack)
            {
                if (FullImageUpload.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(FullImageUpload.FileName).ToLower();
                    String[] allowedExtensions = { ".png" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        try
                        {
                            Bitmap fullimage = new Bitmap(FullImageUpload.FileContent);

                            if ((fullimage.Size.Height == 600) && (fullimage.Size.Width == 800))
                            {
                                if (fileExtension == allowedExtensions[i])
                                {
                                    FullFileOK = true;
                                }
                            }
                            else
                            {
                                FullImageResolution = true;

                            }
                        }
                        catch
                        {
                            FullImageUploadStatus.Text = "Error";
                        }
                    }
                }

                if (FullFileOK)
                {
                    try
                    {
                        FullImageOk = true;
                    }
                    catch (Exception exf)
                    {
                        FullImageUploadStatus.Text = "Error";
                    }
                }
                else
                {
                    if (FullImageResolution)
                    {
                        FullImageUploadStatus.Text = "Size of full image must be 800х600 px.";
                    }
                    else
                    {
                        FullImageUploadStatus.Text = "Error";
                    }

                }
            }

            if (PreviewImageOK == true)
            {
                if (FullImageOk == true)
                {
                        Guid NewPreviewImageFileName = Guid.NewGuid();
                        String fileExtension = Path.GetExtension(PreviewImageUpload.FileName);
                        String fullPreviewImageFileName = PreviewPath + NewPreviewImageFileName + fileExtension;
                        String ShortPreviewImageFileName = NewPreviewImageFileName + fileExtension;
                        PreviewImageUpload.PostedFile.SaveAs(fullPreviewImageFileName);             

                        Guid NewFullImageFileName = Guid.NewGuid();
                        String FullImagefileExtension = Path.GetExtension(FullImageUpload.FileName);
                        String FullFullImageFileName = FullPath + NewFullImageFileName + fileExtension;
                        String ShortFullImageFileName = NewFullImageFileName + fileExtension;
                        FullImageUpload.PostedFile.SaveAs(FullFullImageFileName);

                        Send.PostBackUrl = "#Successfully";

                        using (SqlConnection conn = new SqlConnection(connStr))
                        {

                            string sql = "INSERT INTO md_Articles ([Title],[Subtitle],[Body],[ImagePreview],[ImageFull],[Approved],[CategoryID],[AddedBy]) VALUES " + "(@Title,@Subtitle,@Body,@ImagePreview,@ImageFull,@Approved,@CategoryID,@AddedBy)";

                            using (SqlCommand cmd = new SqlCommand(sql, conn))
                            {

                                SqlParameter[] prms = new SqlParameter[8];
                                prms[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 256);
                                prms[0].Value = TextBoxTitle.Text.Trim();
                                prms[1] = new SqlParameter("@Subtitle", SqlDbType.NText);
                                prms[1].Value = TextBoxSubtitle.Text.Trim();
                                prms[2] = new SqlParameter("@Approved", SqlDbType.Bit);
                                prms[2].Value = Approved;
                                prms[3] = new SqlParameter("@Body", SqlDbType.NText);
                                prms[3].Value = Article.Text.Trim();
                                prms[4] = new SqlParameter("@ImagePreview", SqlDbType.NVarChar, 256);
                                prms[4].Value = ShortPreviewImageFileName;
                                prms[5] = new SqlParameter("@ImageFull", SqlDbType.NVarChar, 256);
                                prms[5].Value = ShortFullImageFileName;
                                prms[6] = new SqlParameter("@CategoryID", SqlDbType.Int, 4);
                                prms[6].Value = Categories.SelectedValue.Trim();
                                prms[7] = new SqlParameter("@AddedBy", SqlDbType.NVarChar, 256);
                                prms[7].Value = AddedBy;
                                cmd.Parameters.AddRange(prms);
                                conn.Open();
                                returnValue = cmd.ExecuteNonQuery();
                                conn.Close();
                            }
                        }
                }
            }
        }
    }
}